<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="名称">
          <el-input
            v-model="searchParam.name"
            placeholder="请输入名称"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="index"
          label="序号"
          width="55"
          align="center"
          key="id"
        />
        <template v-for="item in tableColumns" :key="item">
          <TableColumn v-if="item.prop && item.isShow" :column="item">
            <template v-for="slot in Object.keys($slots)" #[slot]="scope">
              <slot :name="slot" v-bind="scope"></slot>
            </template>
          </TableColumn>
        </template>
        <!-- <el-table-column
          label="名称"
          prop="name"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="尺寸mm"
          prop="size"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="重量/kg"
          prop="weight"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="图片"
          prop="img"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <div class="titleBox" v-if="scope.row.img">
              <div class="demo-image__preview img">
                <el-image
                  style="width: 60px; height: 40px"
                  :src="scope.row.imgback"
                  :preview-src-list="[scope.row.imgback]"
                  fit="cover"
                  preview-teleported
                  :z-index="1000"
                />
              </div>
            </div>
            <div style="width: 60px; height: 40px" v-else></div>
          </template>
        </el-table-column>
        <el-table-column
          label="安全库存"
          prop="safetyStock"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="使用工序"
          prop="usageProcess"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="装载数量+3%"
          prop="loadingQuantity"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="保质期/天"
          prop="qualityGuaranteePeriod"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="是否回收"
          prop="isRecycle"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="{ row }">
            <el-tag v-if="row.isRecycle === 1">是</el-tag>
            <el-tag v-else>否</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="创建人"
          prop="createdUserName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="更新时间"
          prop="lastUpdateTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="更新人"
          prop="lastUpdatedUserName"
          min-width="100"
          show-overflow-tooltip
        /> -->
        <el-table-column fixed="right" label="操作" width="210" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <mesironbaseinfo-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
    <!-- 列设置 -->
    <ColSetting
      v-model:col-setting="tableColumns"
      v-model:search-setting="searchColumns"
      @search-handle="(e:boolean) => (showSearch = e)"
    />
  </div>
</template>

<script setup lang="tsx">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import { h } from "vue";
import request from "@/utils/request";

defineOptions({
  name: "MESMESIRONBASEINFO",
  inheritAttrs: false,
});
// 图片地址
const actionUrl = import.meta.env.VITE_APP_BASE_API;
// 请求参数
const requestApi = "/mes/mesironbaseinfo/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  name: "",
};

const deleteParam = {
  api: "/mes/mesironbaseinfo/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = (res: any) => {
  res.forEach((item: any) => {
    let imgback = "";
    const arr = item.img?.split("/");
    if (arr) {
      const fileName = arr[arr.length - 1] || "";
      const folderName = arr[arr.length - 2] || "";
      if (folderName && fileName) {
        imgback = `${actionUrl}/system/fileCenter/preview/${folderName}/${fileName}`;
      }
      item.imgback = imgback;
    }
  });
  return res;
};
// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/mes/mesironbaseinfo/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开冷铁基础信息表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改冷铁基础信息";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增冷铁基础信息";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开冷铁基础信息表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "冷铁基础信息详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const res = await request({
      url: `/mes/mesironbaseinfo/deleteItem/${id}`,
      method: "post",
    });
    if (res.code === 200) {
      ElMessage.success("删除成功");
      search();
    }
  });
}

const tableColumns = reactive([
  {
    prop: "name",
    label: "名称",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "size",
    label: "尺寸mm",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "weight",
    label: "重量/kg",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "img",
    label: "图片",
    minWidth: 100,
    isShow: true,
    align: "center",
    render: (scope: any) => (
      <el-image
        style="width: 60px; height: 40px"
        src={scope.row.imgback}
        preview-src-list={[scope.row.imgback]}
        fit="cover"
        preview-teleported
        z-index={1000}
      />
    ),
  },
  {
    prop: "safetyStock",
    label: "安全库存",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "usageProcess",
    label: "使用工序",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "loadingQuantity",
    label: "装载数量+3%",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "qualityGuaranteePeriod",
    label: "保质期/天",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "isRecycle",
    label: "是否回收",
    minWidth: 100,
    isShow: true,
    align: "center",
    render: (scope: any) =>
      scope.row.isRecycle === 1 ? <el-tag>是</el-tag> : <el-tag>否</el-tag>,
  },

  {
    prop: "createTime",
    label: "创建时间",
    minWidth: 160,
    isShow: true,
    align: "center",
  },
  {
    prop: "createdUserName",
    label: "创建人",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
  {
    prop: "lastUpdateTime",
    label: "更新时间",
    minWidth: 160,
    isShow: true,
    align: "center",
  },
  {
    prop: "lastUpdatedUserName",
    label: "更新人",
    minWidth: 100,
    isShow: true,
    align: "left",
  },
]);

//  render: (scope: any) =>
//       scope.row.repairLevel == "1" && scope.row.repairLevel * 1 < 4 ? (
//         <el-tag type="danger">
//           {useDictionary("EamFaultLevel", scope.row.repairLevel)}
//         </el-tag>
//       ) : scope.row.repairStatus * 1 >= 4 ? (
//         <el-tag type="info" type="success">
//           {useDictionary("EamFaultLevel", scope.row.repairLevel)}
//         </el-tag>
//       ) : scope.row.repairLevel !== "1" && scope.row.repairStatus * 1 < 4 ? (
//         <el-tag>{useDictionary("EamFaultLevel", scope.row.repairLevel)}</el-tag>
//       ) : (
//         <div></div>
//       ),
const searchColumns = ref([]);
</script>
